<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *公告管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';

if($_RQ['op']=='list'){
	
	$return['list']['0']['totalprice']=pdo_getsum('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'deleted'=>0),'price');
	$return['list']['0']['total']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'deleted'=>0));
	$return['list']['0']['totalmember']=pdo_count('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid']));
	$m=pdo_fetch('select count(1) from '.tablename('jy_weishop_order').' where  uniacid = '.$_SESSION['uniacid'].' and ispay=1 and deleted=0 group by mid');
	$return['list']['0']['xfmember']=$m['count(1)']?$m['count(1)']:0;
	if($return['list']['0']['totalmember']>0){
		$return['list']['0']['buy']=price($return['list']['0']['xfmember']/$return['list']['0']['totalmember']*100);		
		$return['list']['0']['xf']=price($return['list']['0']['total']/$return['list']['0']['totalmember']*100);
	}
	$htmls=array(
		'type'=>'list',	
		'list'=>array('data'=>'list','delall'=>false,'del_one'=>false,'edit'=>false,'list'=>array(
			array('name'=>'totalprice','title'=>'订单总金额(已支付)','type'=>'string'),
			array('name'=>'total','title'=>'总订单数(已支付)','type'=>'string'),			
			array('name'=>'totalmember','title'=>'总会员数','type'=>'string'),
			array('name'=>'xfmember','title'=>'消费会员数','type'=>'string'),
			array('name'=>'buy','title'=>'会员消费率(%)','type'=>'string'),
			array('name'=>'xf','title'=>'订单购买率(%)','type'=>'string'),
		)),
		
	);
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_store_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	
	$return['status']=array('0'=>'禁用','1'=>'启用');
	$return['store']=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['role']=pdo_getall('jy_weishop_store_role',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),array('id','name'));
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回角色列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'name','title'=>'姓名','type'=>'string'),
					array('name'=>'mobile','title'=>'电话','type'=>'string'),
					array('type'=>'choose','title'=>'选择会员','name'=>'mid','action'=>'store.get_member','choose_title'=>'选择会员'),
					array('type'=>'select','title'=>'选择门店','name'=>'storeid','options'=>'store' ),
					array('type'=>'select','title'=>'选择角色','name'=>'roleid','options'=>'role','option_title'=>'name'),
					array('name'=>'getmessage','title'=>'门店推送消息','type'=>'radio','options'=>'status'),
					array('name'=>'getnotice','title'=>'门店短信通知','type'=>'radio','options'=>'status'),
					array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
				)
			),
		)
	);
	$return['htmls']=$htmls;
	
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array_fields(array('name','mobile','mid','storeid','roleid','getmessage','getnotice','status'),$postdata);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_weishop_store_worker',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_weishop_store_worker',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_weishop_store_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的角色！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_weishop_store_worker',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}